package org.dru.clay.util.file;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.dru.clay.util.functional.Function;

/**
 * LazyFileSet
 * User: joakimd
 * Date: 2013-07-24
 * Time: 13:00
 */
public class LazyFileSet extends AbstractFileSet {
    private final Function<File, Collection<File>> function;
    private Collection<File> files;

    public LazyFileSet(final File dir, final Function<File, Collection<File>> function) {
        super(dir);
        function.getClass();
        this.function = function;
    }

    public Function<File, Collection<File>> getFunction() {
        return function;
    }

    public Collection<File> getFiles() {
        if (files == null) {
            files = function.apply(getDir());
        }
        return files;
    }

    @Override
    public Iterator<File> iterator() {
        return new ArrayList<File>(getFiles()).iterator();
    }
}
